package com.whfc.ms.api.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.param.AppFacaGateEditParam;
import com.whfc.entity.dto.Apply.rep.*;
import com.whfc.entity.dto.Apply.req.*;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.ms.api.sys.dto.EntryApplicationDTO;
import com.whfc.ms.api.sys.dto.EntryApplicationDetailRep;
import com.whfc.ms.api.sys.service.SysApplyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author xjy
 * @date 2025-09-13
 * 人员申请 and 审批
 */
@RestController
@RequestMapping("/mp/api/apply")
public class SysApplyController extends BaseController {
    @Autowired
    private SysApplyService sysApplyService;


    @PostMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,@RequestBody AppEntryApplicationReq request) {
        PageData<EntryApplicationListRep> pageData = sysApplyService.list(user,request);
        return ResultUtil.success(pageData);
    }

    /**
     * 获取审核列表
     * @return
     */
    @GetMapping("/work/queryReviewList")
    public Result queryReviewList(@SessionAttribute(SessionAttr.MS_USER) SysUser user, AppEntryApplicationReq req) {
        PageData<EntryApplicationDTO> institutions = sysApplyService.entryReview(user, req);
        return ResultUtil.success(institutions);
    }

    /**
     * 人员申请方法
     * 该方法用于处理人员申请相关业务逻辑。
     *
     * @return 描述申请操作响应的字符串
     */
    @PostMapping("/work/personnel")
    public Result saveApplyPersonnel(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody AppEntryApplicationDTO req) {
        String string = sysApplyService.saveApplyPersonnel(user, req);
        if(!Objects.equals(string,"提交成功")){
            return ResultUtil.failure("提交失败");
        }
        return ResultUtil.success();
    }

    /**
     * 修改入场申请
     *
     * @param user
     * @param req
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody AppUpEntryApplicationDTO req) {
        sysApplyService.edit(req);
        return ResultUtil.success();
    }



    /**
     * 获取工种
     * @return
     */
    @GetMapping("/work/queryCategory")
    public Result queryCategory(@SessionAttribute(SessionAttr.MS_USER) SysUser user) {
        logger.info("APPMPUA|{}",
                user.getId());
        return ResultUtil.success(sysApplyService.queryCategory(user.getId()));
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping("/work/uplodFile")
    public Result uplodFile(@RequestParam("file") MultipartFile file) {
        return ResultUtil.success(sysApplyService.uplodFile(file));
    }
    /**
     * 根据人员姓名查找人员
     * 入场人员列表
     * @param req
     * @return
     */
    @PostMapping("/work/queryPersonnel")
    public Result queryPersonnel(@RequestBody QueryPersonnelReq req) {
       List<SafetyTrainingExamReq> examReqList = sysApplyService.queryPersonnel(req);
       return ResultUtil.success(examReqList);
    }

    /**
     * 获取机构
     * @return
     */
    @GetMapping("/work/queryInstitution")
    public Result queryInstitution(@SessionAttribute(SessionAttr.MS_USER) SysUser user,Integer deptId) {
        System.out.println(JSONObject.toJSONString(user));
        List<InstitutionRep> institutions = sysApplyService.queryInstitution(user,deptId);
        return ResultUtil.success(institutions);
    }




    /**
     * 入场审核详情
     * @return
     */
    @GetMapping("/work/queryEntryDetails")
    public Result queryEntryDetails(@RequestParam("id") Integer id) {
        EntryApplicationDetailRep institutions = sysApplyService.queryEntryDetails(id);
        return ResultUtil.success(institutions);
    }

    /**
     * 入场详情
     * @return
     */
    @GetMapping("/work/queryEntryInfo")
    public Result queryEntryInfo( @RequestParam("id") Integer id) {
        AppEntryApplicationRep applicationRep = sysApplyService.queryEntryInfo(id);
        return ResultUtil.success(applicationRep);
    }
    /**
     * 审核
     *
     * @param id
     * @return
     */
    @GetMapping("/work/entryReview")
    public Result applyEntryReview(@SessionAttribute(SessionAttr.MS_USER) SysUser user,@RequestParam("approvalResult") Integer approvalResult, @RequestParam("id") Integer id) {
        String review = sysApplyService.applyEntryReview(user,approvalResult, id);
        if(Objects.equals(review,"审核成功!")){
            return ResultUtil.success(review);
        }else {
            return ResultUtil.failure(review);
        }
    }
}
